
package com.bluelinelabs.conductor.demo.controllers;

import com.bluelinelabs.conductor.conductor.manager.ObjectManager;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;

import com.bluelinelabs.conductor.conductor.Controller;
import com.bluelinelabs.conductor.conductor.Router;
import com.bluelinelabs.conductor.conductor.RouterTransaction;
import com.bluelinelabs.conductor.demo.ResourceTable;
import com.bluelinelabs.conductor.demo.controllers.base.BaseController;
import com.bluelinelabs.conductor.viewpager.RouterPagerProvider;

import java.util.Locale;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class ViewPagerController extends BaseController {


    private int[] pageColors = {ResourceTable.Color_green_300, ResourceTable.Color_cyan_300, ResourceTable.Color_deep_purple_300, ResourceTable.Color_lime_300, ResourceTable.Color_red_300};

    private RouterPagerProvider pagerAdapter = new RouterPagerProvider(this) {
        @Override
        public int getCount() {
            return pageColors.length;
        }

        @Override
        public String getPageTitle(int position) {
            return "Page " + position;
        }

        @Override
        public void configureRouter(Router router, int position) {
            if (!router.hasRootController()) {
                IntentParams intentParams = new IntentParams();
                intentParams.setParam(ChildController.KEY_TITLE, String.format(Locale.getDefault(), "Child #%d (Swipe to see more)", position));
                intentParams.setParam(ChildController.KEY_BG_COLOR, getView().getContext().getString(pageColors[position]));
                intentParams.setParam(ChildController.KEY_COLOR_IS_RES, true);

                Controller page = new ChildController(intentParams);
                router.setRoot(RouterTransaction.with(page));
            }
        }
    };

    public ViewPagerController() {
        super(ResourceTable.Layout_controller_view_pager);
    }

    @Override
    public void onViewCreated(Component component) {
        title = "ViewPager Demo";
        ObjectManager.getInstance().setIdTitleAboutSetVisibility(Component.HIDE);
        PageSlider Id_view_pager = (PageSlider) component.findComponentById(ResourceTable.Id_view_pager);
        TabList Id_tab_layout = (TabList) component.findComponentById(ResourceTable.Id_tab_layout);

        Id_view_pager.setProvider(pagerAdapter);

        for (int i = 0; i < pageColors.length; i++) {
            TabList.Tab tab = Id_tab_layout.new Tab(getActivity());
            tab.setText("PAGE " + i);
            Id_tab_layout.addTab(tab);
        }
        Id_tab_layout.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                int currentPage = Id_view_pager.getCurrentPage();
                if (currentPage != tab.getPosition()) {
                    Id_view_pager.setCurrentPage(tab.getPosition());
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                System.out.println("wtf onUnselected ");
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                System.out.println("wtf onReselected ");
            }
        });
        Id_tab_layout.selectTabAt(0);


        Id_view_pager.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int position, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int position) {
                int currentPage = Id_view_pager.getCurrentPage();
                if (Id_tab_layout.getSelectedTabIndex() != currentPage) {
                    Id_tab_layout.selectTabAt(currentPage);
                }
            }

            @Override
            public void onPageChosen(int position) {

            }
        });
    }


}
